<?php
class Products_model extends Model{

	var $_datagrid_name = 'dt1';
	
    function Products_model(){
        parent::Model();       
    }
	function getdatasource(){ 
    	$page = $this->input->post($this->_datagrid_name.'_hidden_page')?$this->input->post($this->_datagrid_name.'_hidden_page'):1;    	
    	$limit = $this->config->item('record_limit');   	
    	$count = $this->db->count_all('product');    	
    	$pages = intval($count/$limit) + ($count%$limit?1:0);
    	$page = $page>$pages?1:$page;
    	$this->db->limit($limit,($page-1)*$limit);
    	$this->db->select('id,image,category_id,bestseller,sortorder');
    	$this->db->order_by('sortorder,id');
    	$query = $this->db->get('product');
    	$dts = $query->result_rowlist();
    	for($i=0;$i<$query->num_rows;$i++){    		
    		$categoryid  = $dts[$i][2];
			#get_content('productcategory',$categoryid)->categoryname // unuse			
    		$dts[$i][1] = base_url().$this->config->item('product_img_path').$dts[$i][1];
    		$dts[$i][2] = get_content('product',$dts[$i][0])->name;			
    	}
    	$datagrid->id = $this->_datagrid_name;
    	$datagrid->page = $page;
    	$datagrid->pages = $pages;
    	$datagrid->datasource = $dts;    	
    	return $datagrid;    	
    }
	function delete(){
    	if($this->input->post($this->_datagrid_name.'_hidden_value')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_value');
    		$this->_deleteimage($id);
    		delete_content('product',$id);
    		$this->db->delete('product',array('id'=>$id));
    	}
    	else if($this->input->post($this->_datagrid_name.'_chk_select')){
    		$id = $this->input->post($this->_datagrid_name.'_chk_select');
    		for($i=0;$i<count($id);$i++){
    			$this->_deleteimage($id[$i]);
    			delete_content('product',$id[$i]);
	    		$this->db->delete('product',array('id'=>$id[$i]));
    		}
    	}
    }
    function addnew(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('product_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    		} 
			$bestseller = ($this->input->post('chk_bestseller')===FALSE)?0:1;
    		$this->db->insert('product',array('image'=>$image_name,'category_id'=>$this->input->post('cbo_category'),'price'=>$this->input->post('txt_price'),'specialprice'=>$this->input->post('txt_specialprice'),'bestseller'=>$bestseller,'sortorder'=>0));
    		set_content('product',$this->db->insert_id(),array('name'=>$this->input->post('txt_name'),'description_short'=>$this->input->post('txt_shortdescription'),'description_long'=>$this->input->post('txt_longdescription')));
    		redirect('products','location');
    	}
    }
    function update(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$image_name = '';
    		$config['upload_path'] = $this->config->item('product_img_path');
			$config['allowed_types'] = $this->config->item('upload_allowed_types');
			$this->load->library('upload', $config);
    		if($this->upload->do_upload('txt_image')){
    			$upload_data = $this->upload->data();
    			$image_name = $upload_data['file_name'];
    			$this->_deleteimage($this->input->post('hidden_id'));
    		}
    		if(!empty($image_name)){
    			$this->db->set('image',$image_name);
    		}
			$bestseller = ($this->input->post('chk_bestseller')===FALSE)?0:1;
    		$this->db->where('id',$this->input->post('hidden_id'));
    		$this->db->update('product',array('category_id'=>$this->input->post('cbo_category'),'price'=>$this->input->post('txt_price'),'specialprice'=>$this->input->post('txt_specialprice'),'bestseller'=>$bestseller,'sortorder'=>0));    		
    		set_content('product',$this->input->post('hidden_id'),array('name'=>$this->input->post('txt_name'),'description_short'=>$this->input->post('txt_shortdescription'),'description_long'=>$this->input->post('txt_longdescription')));
			redirect('products','location');
    	}
    }
    function getdetail($id){
    	$query = $this->db->get_where('product',array('id'=>$id));
    	if($query->num_rows){
    		$product = $query->row();
    		$productcontent = get_content('product',$id);
    		$detail->id = $id;
    		$detail->image = $product->image;
    		$detail->price = $product->price;
			$detail->specialprice = $product->specialprice;
			$detail->bestseller = $product->bestseller;									
    		$detail->categoryid = $product->category_id;
    		$detail->name = $productcontent->name;
	    	$detail->shortdescription = $productcontent->description_short;
	    	$detail->longdescription = $productcontent->description_long;
	    	return $detail;
    	}
    	else return FALSE;
    } 
    function getcategorylist(){
    	$this->db->order_by('superid,sortorder,id');
		$this->db->where('superid != 0');
    	$query = $this->db->get('productcategory');
    	$category = array();
    	$res = $query->result();
    	for($i=0;$i<$query->num_rows();$i++){
    		$category[$i]->id = $res[$i]->id;
    		$category[$i]->name = get_content('productcategory',$res[$i]->id)->categoryname;
    	}
    	return $category;
    }   
	function save(){
    	if(FALSE!==$this->input->post('hidden_status')){
    		$id = $this->input->post($this->_datagrid_name.'_hidden_id');
    		// save sortorder
			$sortorder = $this->input->post($this->_datagrid_name.'_column_4');
    		for($i=0;$i<count($id);$i++){
    			$this->db->where('id',$id[$i]);
    			$this->db->update('product',array('sortorder'=>$sortorder[$i]));
    		}
			// save bestseller
			if(is_array($id)){
				$strid = implode(",",$id);
				$this->db->where("id IN($strid)");				
				$this->db->update('product',array('bestseller'=>0));				
				$bestseller = $this->input->post($this->_datagrid_name.'_column_3');
				if(is_array($bestseller)){
					$strbestseller = implode(",",$bestseller);
					$this->db->where("id IN($strbestseller)");					
					$this->db->update('product',array('bestseller'=>1));	
				}
			}
    	}
    }
	function _deleteimage($id){
    	$res = $this->db->get_where('product',array('id'=>$id));
    	$image = $res->row()->image;
    	if(file_exists($this->config->item('product_img_path').$image)){
    		@unlink($this->config->item('product_img_path').$image);
    	}
    }
}

/* End of file */